package org.springblade.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.application.command.DashboardAndDisplaysCommand;
import org.springblade.application.command.TargetAddressPageCommand;
import org.springblade.application.mapper.TargetAddressMapper;
import org.springblade.application.service.IApplicationManagementService;
import org.springblade.application.service.ITargetAddressService;
import org.springblade.application.util.HttpClientUtil;
import org.springblade.applicationapi.entity.ApplicationManagementEntity;
import org.springblade.applicationapi.entity.TargetAddressEntity;
import org.springblade.applicationapi.vo.TargetAddressVO;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.entity.Dept;
import org.springblade.system.feign.ISysClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wzy
 * @Date 2021/12/13
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class TargetAddressServiceImpl extends BaseServiceImpl<TargetAddressMapper, TargetAddressEntity> implements ITargetAddressService {
@Autowired
private IApplicationManagementService managementService;

@Resource
private ISysClient sysClient;

	@Override
	public IPage<TargetAddressVO> addressPage(TargetAddressPageCommand pageCommand, IPage<TargetAddressVO> page){
//1、查用户所在部门 及所有的父级部门 2、查所有的应用管理数据
		log.info("addressPage==>" + "current user = " + AuthUtil.getUser().toString());
		//查用户所在部门 及所有的父级部门
		Long deptId1 = Long.valueOf(AuthUtil.getDeptId());


		//递归查部门id
		R<List<Dept>> deptList = sysClient.getDeptList();
		String deptIdString = this.getDeptIdString(deptList,deptId1);
		deptIdString= deptIdString +"," + deptId1+",";
		List<Long> deptIdlongList = Func.toLongList(deptIdString);

		Set<ApplicationManagementEntity> managementEntityList1 = new HashSet<>();
		for(Long longId : deptIdlongList){
			List<ApplicationManagementEntity> managementEntityList=baseMapper.getEntity(longId);
			managementEntityList1.addAll(managementEntityList);
		}

		List<Long> targetIdLong = managementEntityList1.stream().map(ApplicationManagementEntity::getTargetAddressId).collect(Collectors.toList());
        pageCommand.setTargetIdLong(targetIdLong);

		return page.setRecords(baseMapper.addressPage(page,pageCommand));
	}

	@Override
	public IPage<TargetAddressVO> unlimitedPage(TargetAddressPageCommand pageCommand, IPage<TargetAddressVO> page){

		return page.setRecords(baseMapper.unlimitedPage(page,pageCommand));
	}

	private String getDeptIdString(R<List<Dept>> deptList , Long deptId1) {
		List<Dept> collect = deptList.getData().stream().filter(dept -> dept.getId().equals(deptId1)).collect(Collectors.toList());
		if (collect != null && collect.size() > 0) {
			Long parentId1 = collect.get(0).getParentId();
			if (!parentId1.equals(0L)) {
				return parentId1 + "," + getDeptIdString(deptList, parentId1);
			}
		}
		return "11111111111";


	}

	@Override
	public Long add(TargetAddressEntity entity)  {
		//新增应用中心
	    entity.setAppId(UUID.randomUUID().toString());
		this.save(entity);
		//新增应用管理
		ApplicationManagementEntity managementEntity = new ApplicationManagementEntity();
		managementEntity.setTargetAddressId(entity.getId());
		managementEntity.setDeptId(AuthUtil.getDeptId());
		managementService.save(managementEntity);
		return entity.getId();
	}
	@Override
	public boolean edit(TargetAddressEntity entity){
		return this.updateById(entity);
	}
   @Override
	public boolean removeByid(List<Long> ids){
		return this.deleteLogic(ids);
   }



   @Override
	public R<Object> configInfo(Long id, String configInfo){
	   TargetAddressEntity targetAddressEntity = baseMapper.selectById(id);
	   targetAddressEntity.setConfigInfo(configInfo);
	   return R.status(this.updateById(targetAddressEntity));
   }


}
